package com.cysyz.mylib.tplgenerator.impl;

import com.cysyz.mylib.tplgenerator.ReplaceAdapter;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by emall on 2014/8/19.
 */
public abstract class AbstractSimpleReplaceAdapter implements ReplaceAdapter {
    protected abstract String getMatchRegex();
    protected abstract String getReplaceRegex();

    @Override
    public StringBuffer process(StringBuffer stringBuffer) {
        Pattern pattern = Pattern.compile(getMatchRegex());
        StringBuffer result = new StringBuffer();
        Matcher matcher = pattern.matcher(stringBuffer.toString());
        while(matcher.find()) {
            matcher.appendReplacement(result, getReplaceRegex());
        }
        matcher.appendTail(result);
        return result;
    }
}
